//
//  HomeViewController.m
//  Cat
//
//  Created by lushitong on 2018/11/29.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "HomeViewController.h"
#import "HttpHomeManager.h"
#import "HomeCommedCollectionView.h"
#import "InviteViewController.h"
#import "SearchViewController.h"
#import "FLPrivacyAlertView.h"
#import "DetailViewController.h"
#import "LoginViewController.h"

@interface HomeViewController ()

@property (nonatomic, strong) UIButton *bottomButtom;
@property (nonatomic, strong) UIImageView *searcgImageView;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) HomeCommedCollectionView *recommedView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	self.topTitleView.backgroundColor = COLOR_WITH_HEX(0xFFC236);
	self.topTitleView.bottomLineView.backgroundColor = [UIColor whiteColor];
	[self loadAllSubViews];
	[self loadScrollView];
	[self checkPrivacy];
	[self.recommedView sendRequestWithCommed:NO];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	
}

- (void)checkPrivacy {
	if (!UDToken || UDIsClickPrivacy) {return;} // 引导图出现完成之后执行对应的方法 本地记录一次 如果已经点击了 就不需要再次点击了
	[FLPrivacyAlertView showInView:ScreenKeyWindow clickAgreeButtonAction:^(FLPrivacyAlertView *view) {
		[view dismiss];
		UDIsClickPrivacy = YES;
	} clickDisAgreeAction:^{
		UDIsClickPrivacy = NO;
	}];
}


- (void)loadAllSubViews {
	[self.topTitleView addSubviews:@[self.bottomButtom,self.searcgImageView,self.infoLabel,self.shareButton]];
	
	[self.bottomButtom mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.topTitleView.mas_left).offset(kQKFitModuleDecimal(12.0));
		make.right.equalTo(self.topTitleView.mas_right).offset(kQKFitModuleDecimal(-72.0));
		make.top.equalTo(self.topTitleView.mas_top).offset(kQKFitModuleDecimal(24.0+ StatusBarSafeInsetHeight));
		make.height.equalTo(@(kQKFitModuleDecimal(28.0)));
	}];
	
	[self.searcgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.bottomButtom.mas_left).offset(kQKFitModuleDecimal(8.0));
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(16.0), kQKFitModuleDecimal(16.0)));
		make.centerY.equalTo(self.bottomButtom.mas_centerY);
	}];
	
	[self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.searcgImageView.mas_right).offset(kQKFitModuleDecimal(8.0));
		make.centerY.equalTo(self.searcgImageView.mas_centerY);
	}];
	
	[self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.bottomButtom.mas_right).offset(kQKFitModuleDecimal(13.0));
		make.centerY.equalTo(self.searcgImageView.mas_centerY);
	}];
}


- (void)loadScrollView {
	[self.view addSubview:self.recommedView];
	
}

- (void)shareButtonAction {
	if (UDToken) {
		InviteViewController *vc = [[InviteViewController alloc] init];
		[self.navigationController pushViewController:vc animated:YES];
	} else {
		LoginViewController *login = [[LoginViewController alloc] init];
		[self.navigationController pushViewController:login animated:YES];
	}
}

- (void)searchAction {
	SearchViewController *search = [[SearchViewController alloc] init];
	[self.navigationController pushViewController:search animated:YES];
}

#pragma mark----- lazy init -----

- (UIButton *)bottomButtom {
	if (!_bottomButtom) {
		_bottomButtom = [UIButton buttonWithType:UIButtonTypeCustom];
		_bottomButtom.backgroundColor = [UIColor whiteColor];
		[_bottomButtom addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
		_bottomButtom.layer.cornerRadius = kQKFitModuleDecimal(2.0);
		_bottomButtom.layer.masksToBounds = YES;
	}
	return _bottomButtom;
}

- (UIImageView *)searcgImageView {
	if (!_searcgImageView) {
		_searcgImageView = [[UIImageView alloc] init];
		_searcgImageView.image = [UIImage imageNamed:@"home_search"];
		_searcgImageView.userInteractionEnabled = YES;
	}
	return _searcgImageView;
}

- (UILabel *)infoLabel {
	if (!_infoLabel) {
		_infoLabel = [UILabel labelWithText:@"搜索宝贝 领券后再购物" font:[UIFont systemFontOfSize:kQKFitModuleDecimal(12)] textColor:COLOR_WITH_HEX(0xBAC0C5)];
		_infoLabel.userInteractionEnabled = YES;
	}
	return _infoLabel;
}

- (UIButton *)shareButton {
	if (!_shareButton) {
		_shareButton  = [UIButton buttonWithTitle:@"分享赚佣" color:COLOR_WITH_HEX(0x4C4947) font:[UIFont systemFontOfSize:kQKFitFont(12)]];
		[_shareButton addTarget:self action:@selector(shareButtonAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _shareButton;
}

- (HomeCommedCollectionView *)recommedView {
	if (!_recommedView) {
		UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
		layout.minimumLineSpacing = 0.0;
		layout.minimumInteritemSpacing = kQKFitModuleFloor(10);
		_recommedView = [[HomeCommedCollectionView alloc] initWithFrame:CGRectMake(0.0, self.topTitleView.bottom, ScreenWidth, ScreenHeight - NavigationBarHeight - TabBarHeight) layout:layout];
	}
	return _recommedView;
}

@end
